package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.UserMapper;
import com.jt.pojo.ItemCat;
import com.jt.pojo.User;
import com.jt.vo.PageResult;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;
    //@Transactional        //事务控制注解，用在增删改操作上，查不需要会拖慢速度
    @Override
    public List<User> findAll(){
        return userMapper.findAll();
    }
    //登录功能
    public String login(User user){
        byte[] bytes=user.getPassword().getBytes();
        String md5psw=DigestUtils.md5DigestAsHex(bytes);
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("password",md5psw);
        queryWrapper.eq("username",user.getUsername());
        User userDB=userMapper.selectOne(queryWrapper);
        if (userDB ==null){
            return null;
        }
        return UUID.randomUUID().toString().replace("-","");
    }
    //获取用户列表
    @Override
    public PageResult getUserList(PageResult pageResult){
        boolean flag= StringUtils.hasLength(pageResult.getQuery());
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(flag,"username",pageResult);
        IPage<User> page=new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        page=userMapper.selectPage(page,queryWrapper);
        long total=page.getTotal();
        List<User> rows=page.getRecords();
        return pageResult.setRows(rows).setTotal(total);
    }
    //修改用户状态
    @Override
    @Transactional
    public void updateUserStatus(Integer id,Boolean status){
        UpdateWrapper<User> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",id);  //约等于 where id=#{id}
        User user=new User();
        user.setStatus(status);
        userMapper.update(user,updateWrapper);  // update user set status=#{status} where id=#{id}
    }
    //新增用户
    @Override
    @Transactional
    public void addUser(User user){
        byte[] bytes=user.getPassword().getBytes();
        String md5psw=DigestUtils.md5DigestAsHex(bytes);
        user.setPassword(md5psw);
        userMapper.insert(user);
    }
    //根据ID获取用户数据
    @Override
    public User getUserById(Integer id){
        return userMapper.selectById(id);
    }
    //根据ID修改用户数据
    @Override
    @Transactional
    public void updateUser(User user){
        userMapper.updateById(user);
    }
    //根据ID删除用户数据
    @Override
    @Transactional
    public void deleteUser(Integer id){
        userMapper.deleteById(id);
    }
}
